package com.heiye.auth.service.Impl;

import cn.hutool.core.util.RandomUtil;
import com.heiye.auth.constant.RedisKeyConstants;
import com.heiye.auth.email.EmailHelper;
import com.heiye.auth.enums.ResponseCodeEnum;
import com.heiye.auth.model.vo.verificationcode.SendEmailVerificationCodeReqVO;
import com.heiye.auth.model.vo.verificationcode.SendPhoneVerificationCodeReqVO;
import com.heiye.auth.sms.AliyunSmsHelper;
import com.heiye.auth.service.VerifyCodeService;
import com.heiye.framework.common.exception.BizException;
import com.heiye.framework.common.response.Response;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Email;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author: heiye
 * @CreateTime: 2024/09/14 下午12:13
 * @Description: 登录接口
 * @Version: 1.0
 */
@Slf4j
@Service
public class VerifyCodeServiceImpl implements VerifyCodeService {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource(name = "taskExecutor")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Resource
    private AliyunSmsHelper aliyunSmsHelper;

    @Resource
    private EmailHelper emailHelper;

    /**
     * 发送手机短信验证码
     *
     * @param sendPhoneVerificationCodeReqVO
     * @return
     */
    @Override
    public Response<?> sendPhoneVerificationCode(SendPhoneVerificationCodeReqVO sendPhoneVerificationCodeReqVO) {
        String phoneNumber = sendPhoneVerificationCodeReqVO.getPhoneNumber();
        // 构建验证码 redis key
        String key = RedisKeyConstants.buildPhoneVerificationCodeKey(phoneNumber);

        // TODO 当前不调用阿里云服务
        // 生成验证码
//        String code = RandomUtil.randomNumbers(6);
//
//        log.info("==> 手机号: {}, 已生成验证码：【{}】", dto.getMobile(), code);
//
//        // 将区号加入到手机号中
//        String phoneNumber = dto.getCountryCode() + dto.getPhoneNumber();
//
//        // 异步发送验证码
//        threadPoolTaskExecutor.submit(() -> aliyunSmsHelper.sendVerifyCode(phoneNumber, code));

        log.info("==> 手机号: {}, 已生成验证码：【{}】", phoneNumber, "123456");

        // 存储验证码到 redis, 并设置过期时间为 5 分钟
        redisTemplate.opsForValue().set(key, "123456", 5, TimeUnit.MINUTES);

        return Response.success();
    }

    /**
     * 发送邮件验证码
     *
     * @param sendEmailVerificationCodeReqVO
     * @return
     */
    @Override
    public Response<?> sendEmailVerificationCode(SendEmailVerificationCodeReqVO sendEmailVerificationCodeReqVO) {
        // 构建验证码 redis key
        String key = RedisKeyConstants.buildEmailVerificationCodeKey(sendEmailVerificationCodeReqVO.getEmail());

        // 判断是否已发送验证码
        Boolean isSent = redisTemplate.hasKey(key);
        if (Boolean.TRUE.equals(isSent)) {
            // 若之前发送的验证码未过期，则提示发送频繁
            throw new BizException(ResponseCodeEnum.VERIFICATION_CODE_SEND_FREQUENTLY);
        }

        // 生成验证码
        String code = RandomUtil.randomNumbers(6);

        log.info("==> 邮箱: {}, 已生成验证码：【{}】", sendEmailVerificationCodeReqVO.getEmail(), code);

        // 异步发送验证码
        threadPoolTaskExecutor.submit(() -> emailHelper.sendVerifyCode(sendEmailVerificationCodeReqVO.getEmail(), code, 10));

        // 存储验证码到 redis, 并设置过期时间为 10 分钟
        redisTemplate.opsForValue().set(key, code, 10, TimeUnit.MINUTES);

        return Response.success();
    }
}
